package cn.neusoft.baibang.web.controller;

import cn.neusoft.baibang.dto.ProductManagementDto;
import cn.neusoft.baibang.service.IProductService;
import cn.neusoft.baibang.domain.Product;
import cn.neusoft.baibang.query.ProductQuery;
import cn.neusoft.baibang.result.JSONResult;
import cn.neusoft.baibang.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    public IProductService productService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Product product){
        if(product.getId()!=null){
                productService.updateById(product);
        }else{
                productService.insert(product);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
            productService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(productService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(productService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody ProductQuery query){
        Page<Product> page = new Page<Product>(query.getPage(),query.getRows());
        page = productService.selectPage(page);
        return JSONResult.success(new PageList<Product>(page.getTotal(),page.getRecords()));
    }

    /**
     * 上架服务
     */
    @RequestMapping(value = "/onLineProduct/{productId}",method = RequestMethod.POST)
    public JSONResult onLineProduct(@PathVariable("productId") Long productId){
        productService.onLineProduct(productId);
        return JSONResult.success();
    }

    /**
     * 下架服务
     */
    @RequestMapping(value = "/offLineProduct/{productId}",method = RequestMethod.POST)
    public JSONResult offLineProduct(@PathVariable("productId") Long productId){
        productService.offLineProduct(productId);
        return JSONResult.success();
    }

    /**
     * 获取热门数据并将其保存到redis中
     */
    @RequestMapping(value = "/popularProducts",method = RequestMethod.GET)
    public JSONResult getPopularProducts(){
        return JSONResult.success(productService.getPopularProducts());
    }

    /**
     * 新增服务产品
     */
    @RequestMapping(value="/addProduct",method= RequestMethod.POST)
    public JSONResult addProduct(@RequestBody @Valid ProductManagementDto dto){
        Long id = dto.getProduct().getId();
        if(id!=null){

//            courseService.updateCourse(dto);
        }else{
            productService.add(dto);
        }
        return JSONResult.success();
    }

}
